import { setItem, getItem } from './utils';
import {
  THQBYKEY as Key, // 游戏记录
  PARTICIPANTSLIST as peopleKey, // 参加人员
  HOSTNAMEKEY as hostNameKey, // 主持人
} from './constant';

// 游戏创建
export const create = (data: any) => {
  const list = getItem(Key) || [];
  list.unshift({
    id: list.length + 1 + '',
    ...data,
  });
  setItem(Key, list);
};

// 游戏详情
export const queryDetails = (id: string) => {
  const list = getItem(Key) || [];
  return list.find((item: any) => item.id === id);
};

// 游戏列表查询
export const queryList = () => {
  return getItem(Key)?.filter((item: any) => item.status !== 'none') || [];
};

// 游戏记录删除
export const deleteFn = (id: string) => {
  const list = getItem(Key)?.map((item: any) => {
    if (item.id === id) {
      return {
        ...item,
        status: 'none',
      };
    }
    return item;
  });
  setItem(Key, list || []);
};

// 参与人员修改
export const editParticipantsList = (data: any) => {
  const list = data.map((item: any, index: any) => {
    return {
      key: index + 1,
      label: item,
    };
  });
  setItem(peopleKey, list);
};

// 参与人员查询
export const queryParticipantsList = () => {
  return getItem(peopleKey) || [];
};

// 主持人修改
export const editHoseNameList = (data: any) => {
  setItem(hostNameKey, data);
};
// 主持人查询
export const queryHoseNameList = () => {
  return getItem(hostNameKey) || [];
};
